package com.javaspeak.algorithm.graph.shortestpath.floydwarshall;

import org.apache.log4j.BasicConfigurator;
import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

import com.javaspeak.algorithm.graph.shortestpath.floydwarshall.FloydWarshallMatrix;

/**
 * @author John Dickerson
 */
public class FloydWarshallMatrixTest {

    int inf = Integer.MAX_VALUE;


    private void assertArraysEqual( int[][]expectedData, int[][] actualData ){

        for ( int i=0; i<actualData.length; i++ ){

            for ( int j=0; j<actualData.length; j++ ){

                org.junit.Assert.assertEquals(
                        expectedData[ i ][ j ], actualData[ i ][ j ] );
            }
        }
    }


    /**
     *           3        2       2
     *      [1]-----[2]------[3]-----[4]
     *       \      / \              /
     *       7\    /4  \3           / 2
     *         \  /     \          /
     *          [5]-----[6]------[7]
     *               1       2
     */
    @Test
    public void testFillWeight(){

        int[][] actualData =
        {
            { 0, 3, inf, inf, 7, inf, inf },
            { 3, 0, 2, inf, 4, 3, inf },
            { inf, 2, 0, 2, inf, inf, inf },
            { inf, inf, 2, 0, inf, inf, 2 },
            { 7, 4, inf, inf, 0, 1, inf },
            { inf, 3, inf, inf, 1, 0, 2 },
            { inf, inf, inf, 2, inf, 2, 0 }
        };

        int[][] expectedData =
        {
            { 0, 3, 5, 7, 7, 6, 8 },
            { 3, 0, 2, 4, 4, 3, 5 },
            { 5, 2, 0, 2, 6, 5, 4 },
            { 7, 4, 2, 0, 5, 4, 2 },
            { 7, 4, 6, 5, 0, 1, 3 },
            { 6, 3, 5, 4, 1, 0, 2 },
            { 8, 5, 4, 2, 3, 2, 0 }
        };

        FloydWarshallMatrix.fillWeight( actualData );

        assertArraysEqual( expectedData, actualData );
    }


    public static void main( String args[] ){

        BasicConfigurator.resetConfiguration();
        BasicConfigurator.configure();

        JUnitCore junitCore = new JUnitCore();
        junitCore.addListener( new TextListener( System.out ) );
        junitCore.run( FloydWarshallMatrixTest.class );
    }
}
